/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tank.views;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import tank.controllers.SoundPlayer;
import tank.controllers.TankPlayController;
import tank.models.Bomb;
import tank.models.GameSettings;
import tank.models.Global;
import tank.models.Item;

/**
 *
 * @author shengchang
 */
public class TankPlayView extends JPanel implements Runnable{
    private TankPlayController controller;

    public TankPlayView(TankPlayController controller) {
        this.controller = controller;
    }
    
    public void paint(Graphics g)
    {
        //draw map
        //draw background of the map
        drawMapBackground(g);
        
        //draw items of the map
        drawMapItems(g);
        //draw bullet of myTank
        drawMyTankBullet(g);   
        
        //Motion chech
        motionCheck();
        
        //draw bomb effect
        drawBombEffect(g);
        
        //draw my tank
        drawMyTank(g);

    }
    
    public void drawMapBackground(Graphics g)
    {
        g.drawImage(Global.map.getBackGroundImage(), 0, 0, GameSettings.gameWindowSizeX, GameSettings.gameWindowSizeY, null);   
    }  
    
    public void drawMapItems(Graphics g)
    {
        for (int i = 0; i < Global.map.getItemList().size(); i++) {
            Item item = Global.map.getItemList().get(i);
            g.drawImage(item.getImage(), (int) item.getX(), (int) item.getY(), null);
        }
    }
    
    public void drawBombEffect(Graphics g)
    {
        if(Global.bombVector.size() !=0)
        {
            for (int i = 0; i < Global.bombVector.size(); i++) 
            {
                g.drawImage(Global.bombEffectVector.get(Global.bombVector.get(i).getIndex()), Global.bombVector.get(i).getX(), Global.bombVector.get(i).getY(), null);
                
                if( Global.bombVector.get(i).getIndex() < 2 )
                    Global.bombVector.get(i).setIndex( Global.bombVector.get(i).getIndex() + 1 );
                else
                {
                    Global.bombVector.get(i).setIndex(0);
                    Global.bombVector.remove(Global.bombVector.get(i));
                }
            }
        }
    }   
    
    public void drawMyTank(Graphics g)
    {

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        AffineTransform gt = g2d.getTransform();
        AffineTransform at = new AffineTransform();

        double vx = Global.myTank.getX() - 25;
        double vy = Global.myTank.getY() - 25;

        int ty = (int) vy;
        int tx = (int) vx;
        
        at.rotate(0, Global.myTank.getX(), Global.myTank.getY());
        g2d.transform(at);
        g2d.drawImage(Global.myTankTypeVector.get(Global.myTankType), (int)vx+25, (int)vy+25, null);
        
        //draw turret
        double mx = MouseInfo.getPointerInfo().getLocation().x - GameSettings.viewStartX;
        double my = MouseInfo.getPointerInfo().getLocation().y - GameSettings.viewStartY;
        double mxp = (mx - Global.myTank.getX()-25);
        double myp = (Global.myTank.getY()+25 - my);    
        double theta = Math.atan2(mxp, myp);
        at = new AffineTransform();
        at.rotate(theta, Global.myTank.getX()+25, Global.myTank.getY()+25);
        g2d.transform(at);
        try {
            g2d.drawImage(ImageIO.read(new File(GameSettings.tankTurret)), (int)vx+25, (int)vy+25, this);
        } catch (IOException ex) {
            Logger.getLogger(TankPlayView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void drawMyTankBullet(Graphics g)
    {
        g.setColor(Color.orange);
        for(int i = 0; i < Global.myTank.getBulletVector().size(); i++)
        {
            if( Global.myTank.getBulletVector().get(i).getIsLive() || Global.myTank.getBulletVector().get(i) != null )
                g.fillOval(Global.myTank.getBulletVector().get(i).getX() - 5, Global.myTank.getBulletVector().get(i).getY() - 5, 10, 10);
            
            if( Global.myTank.getBulletVector().get(i).getIsLive() == false )
                Global.myTank.getBulletVector().remove(Global.myTank.getBulletVector().get(i));
        }
    }
    
    public void motionCheck()
    {
        /*Check bullet motion with the items in the map*/
        /*We put my tank motion check in tank class */
        int bulletNumber = Global.myTank.getBulletVector().size();
        int bulletX;
        int bulletY;
        int itemX;
        int itemY;
        if( bulletNumber != 0)
        {
            synchronized(this)
            {
                for( int i = 0; i < Global.myTank.getBulletVector().size(); i++ )
                { 
                    bulletNumber = Global.myTank.getBulletVector().size();
                    bulletX = Global.myTank.getBulletVector().get(i).getX() + 5;
                    bulletY = Global.myTank.getBulletVector().get(i).getY() + 5;
                    int itemsNumber = Global.map.getItemList().size();
                    for( int j = 0; j < Global.map.getItemList().size(); j++ )
                    {
                        itemsNumber = Global.map.getItemList().size();
                        itemX = Global.map.getItemList().get(j).getX();
                        itemY = Global.map.getItemList().get(j).getY();
                        Item item = Global.map.getItemList().get(j);
                        if( bulletX > itemX && bulletX < itemX + 50 && bulletY > itemY && bulletY < itemY + 50)
                        {
                            //remove bullet from vector
                            Global.myTank.getBulletVector().remove(Global.myTank.getBulletVector().get(i));
                            //remove item from vector
                            Global.map.getItemList().remove(item);

                            //play sound effect
                            new SoundPlayer(GameSettings.bombAudio).start();
                            
                            //set bombVector
                            Global.bombVector.add(new Bomb(itemX, itemY));
                        }
                    }
                }
            }
        }
    }

    @Override
    public void run() {
        while(true)
        {
            try {
                Thread.sleep(30);
            } catch (InterruptedException ex) {
                Logger.getLogger(TankPlayView.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            this.repaint();
        }
    }
}
